// Encypt.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <io.h>

#define BUFFER 1000000	// File read buffer

int Encrypt(const char *srcFile, const char *destFile, const DWORD szTotal)
{
	FILE *fpSrc = fopen(srcFile, "rb");
	if (!fpSrc)
	{
		printf("Error opening source file\n");
		return 0;
	}

	FILE *fpDest = fopen(destFile, "wb");
	if (!fpDest)
	{
		printf("An error occurred while opening the target file\n");
		return 0;
	}

	DWORD szRead;
	double szEncrypt = 0;
	double pecent=0;

	char fileBuf[BUFFER];
	while ((szRead = fread(fileBuf, 1, BUFFER, fpSrc)) > 0)
	{
		for (int i = 0; i < szRead; i++)
		{
			fileBuf[i] ^= 'G';
		}

		fwrite(fileBuf, 1, szRead, fpDest);

		memset(fileBuf, 0, sizeof(fileBuf));
		szEncrypt += szRead;
		pecent = szEncrypt * 100 / szTotal;

		printf("Progress of %s: %.0f/%d [%3.0f%%]\r", srcFile, szEncrypt, szTotal, pecent);
	}

	printf("\n");

	fclose(fpSrc);
	fclose(fpDest);
	return 1;
}

/**
	Iterate Through all the specified types of files in the folder
	dirPath:The path to the specified folder
	type:�����ļ���������[*.* | *.jpg.....]
**/
void ListDirectory(const char *dirPath, const char *type, bool bEncrypt)
{
	_finddata_t findData; //�ļ���Ϣ�ṹ��
	
	DWORD handle;
	DWORD count = 0;

	char srcFile[_MAX_FNAME] = { 0 };
	char destFile[_MAX_FNAME]; 

	sprintf(srcFile, "%s\\%s", dirPath, type); 

	handle = _findfirst(srcFile, &findData); 
	if (handle == -1)
	{
		printf("None one %s in %s\n", type, dirPath);
		return;
	}

	do
	{
		if (findData.attrib & _A_SUBDIR | findData.attrib & _A_HIDDEN | findData.attrib & _A_SYSTEM) 
		{
			continue;
		}

		memset(srcFile, 0, _MAX_FNAME);
		memset(destFile, 0, _MAX_FNAME);

		if (bEncrypt) //If bEncrypt == true began encrypt
		{
			sprintf(srcFile, "%s\\%s", dirPath, findData.name);
			sprintf(destFile, "%s\\%s.bak", dirPath, findData.name);
		}
		else		//If bEncrypt == false began decrypt
		{
			char fName[_MAX_FNAME];
			sprintf(srcFile, "%s\\%s", dirPath, findData.name);
			_splitpath(srcFile, 0, 0, fName, 0);	// Delete .bak extend name
			sprintf(destFile, "%s\\%s", dirPath, fName);
		}
		Encrypt(srcFile, destFile, findData.size);

		count++;

	} while (_findnext(handle, &findData) == 0);

	_findclose(handle); // Close Ergodic handle

	printf("\n------------------------- All %d files were encrypted -------------------------\n", count);
}

// Program entry
int main(int argc, char *argv[])
{
//	char path[_MAX_PATH];
	char extend[_MAX_EXT]={ 0 };

	if(argc < 3)
	{
		char selfName[_MAX_FNAME];
		_splitpath(argv[0], NULL, NULL, selfName, NULL);
		printf("useage: %s <directory full path> <extend name> [-e|-d]\n",selfName);

		return 1;
	}

	sprintf(extend,"*.%s",argv[2]);

	printf("\n------------------------- Began to encrypt -------------------------\n");

	if(argv[3] != NULL && strcmp(argv[3], "-d") == 0)
	{
		ListDirectory(argv[1], extend, false);		// decrypt
	}
	else
	{
		ListDirectory(argv[1], extend, true);		// encrypt
	}

	return 0;
}
